/**
**********************************************************************
* @file     main.c
* @brief    对文件的简要说明
* @details  对文件的详细说明
* @author   jcai
* @date     2022-05-05 09:06:06
* @copyright Copyright &copy; 2022 wy1107412334@163.com All rights reserved
* @version  v0.0.1
*
* \n Email:  wy1107412334@163.com
* \n Blog:   None
* \n Github: https://github.com/wy1107412334
* \n Gitee:  https://gitee.com/git.git1107412334.com
*
**********************************************************************
* @note
* None.
**********************************************************************
* @attention
* None.
**********************************************************************
@verbatim
@endverbatim
**********************************************************************
* @par 历史记录
* 1. v0.0.1: jcai, 2022-05-05 09:06:06
*   - 首次编写
*
*<center>Copyright &copy; 2022 wy1107412334@163.com All rights reserved</center>
**********************************************************************
*/

/* 头文件导入区 --------------------------------------------------- */

#include <SDL/SDL_active.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_video.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_events.h>
#include <SDL/SDL_timer.h>
#include <SDL/SDL_keysym.h>
#include <SDL/SDL_keyboard.h>


typedef struct
{
    int windowed;
    int windowOK;
} Win_t;

// 窗口属性
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

// 将要使用的表面
SDL_Surface *testing= NULL;
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;

// 事件结构体
SDL_Event event;

// 使用的字体
TTF_Font *font = NULL;
// 字体颜色
SDL_Color textColor = {0x77, 0x77, 0x77};


SDL_Surface *load_image(const char *file)
{
    SDL_Surface *loadedImage = NULL;
    SDL_Surface *optimizedImage = NULL;

    // 通过 SDL_imag 模块加载图片
    loadedImage = IMG_Load(file);
    if (!loadedImage)   return NULL;

    optimizedImage = SDL_DisplayFormat(loadedImage);
    SDL_FreeSurface(loadedImage);
    if (optimizedImage)
    {
        // 映射关键色
        uint32_t keycolor = SDL_MapRGB(optimizedImage->format, 0, 0xff, 0xff);
        // 将所有颜色为 RGB(0x00, 0xff, 0xff) 的像素设为透明
        SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, keycolor);
    }

    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface *src, SDL_Surface *dst)
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface(src, NULL, dst, &offset);
}

int init()
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
        return -1;

    if (TTF_Init() == -1)
    {
        SDL_Quit();
        return -2;
    }

    return 0;
}

int load_files()
{
    // 加载背景图片
    testing = load_image("window.png");
    if (!testing)   return -1;

    font = TTF_OpenFont("lazy.ttf", 32);
    if (!font)
    {
        SDL_FreeSurface(testing);
        return -2;
    }

    return 0;
}

void clean_up()
{
    SDL_FreeSurface(testing);

    TTF_CloseFont(font);
    TTF_Quit();

    SDL_Quit();
}


int win_init(Win_t *w)
{
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
                              SDL_SWSURFACE | SDL_RESIZABLE);
    if (!screen)
    {
        w->windowOK = 0;
        return -1;
    }
    w->windowOK = 1;

    SDL_WM_SetCaption("Window Event Test", NULL);
    w->windowed = 1;
    return 0;
}
void win_display_message(const char *str)
{
    if (message)
    {
        SDL_FreeSurface(message);
        message = NULL;
    }
    // 渲染字体
    if (str)
    {
        message = TTF_RenderText_Solid(font, str, textColor);
        printf("%s: %s\n", __func__, str);
    }
}
int win_toggle_fullscreen(Win_t *w)
{
    const char *str = NULL;

    if (w->windowed)
    {
        screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
                              SDL_SWSURFACE | SDL_RESIZABLE | SDL_FULLSCREEN);
        if (!screen)
        {
            w->windowOK = 0;
            return -1;
        }
        w->windowed = 0;
        str = "enter Full screen";
    }
    else
    {
        screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
                              SDL_SWSURFACE | SDL_RESIZABLE);
        if (!screen)
        {
            w->windowOK = 0;
            return -2;
        }
        w->windowed = 1;
        str = "leave Full screen";
    }

    win_display_message(str);

    return 0;
}
int win_handle_events(Win_t *w)
{
    if (!w->windowOK)   return -1;

    // 调整窗口大小
    if (event.type == SDL_VIDEORESIZE)
    {
        screen = SDL_SetVideoMode(event.resize.w, event.resize.h,
                                  SCREEN_BPP, SDL_SWSURFACE | SDL_RESIZABLE);
        if (!screen)
        {
            w->windowOK = 0;
            return -2;
        }
    }
    // Enter 键被按下
    else if ((event.type == SDL_KEYDOWN) &&
             (event.key.keysym.sym == SDLK_RETURN))
    {
        if (win_toggle_fullscreen(w))
            return -3;
    }
    else if (event.type == SDL_ACTIVEEVENT)
    {
        const char *str = NULL;

        if (event.active.state & SDL_APPACTIVE)
        {
            if (event.active.gain == 0)
            {
                SDL_WM_SetCaption("Window Event Test: Iconified", NULL);
                str = "Iconified";
            }
            else
            {
                SDL_WM_SetCaption("Window Event Test", NULL);
                str = "";
            }
            win_display_message(str);
        }
        else if (event.active.state & SDL_APPINPUTFOCUS)
        {
            if (event.active.gain == 0)
            {
                SDL_WM_SetCaption("Window Event Test: Keyboard focus lost",
                                  NULL);
                str = "Keyboard focus lost";
            }
            else
            {
                SDL_WM_SetCaption("Window Event Test", NULL);
                str = "";
            }
            win_display_message(str);
        }
        else if (event.active.state & SDL_APPMOUSEFOCUS)
        {
            if (event.active.gain == 0)
            {
                SDL_WM_SetCaption("Window Event Test: Mouse focus lost", NULL);
                str = "Mouse focus lost";
            }
            else
            {
                SDL_WM_SetCaption("Window Event Test", NULL);
                str = "";
            }
            win_display_message(str);
        }
    }
    else if (event.type == SDL_VIDEOEXPOSE)
    {
        if (SDL_Flip(screen) == -1)
        {
            w->windowOK = 0;
            return -4;
        }
    }

    return 0;
}
int win_error(const Win_t *w)
{
    return !w->windowOK;
}


/**
**********************************************************************
* @brief    简要说明
* @details  详细说明
* @param    none
* @param[in]     a: 该参数用于输入
* @param[in,out] b: 该参数用于输入和输出
* @param[out]    c: 该参数用于输出
* @retval  none
* @retval  0 函数成功
* @retval  1 函数失败
* @return  返回0成功；否则失败
* @note    none
*/
int main(int argc, char *argv[])
{
    Win_t w;
    int quit = 0;

    if (init() != 0)
    {
        fprintf(stderr, "SDL初始化失败\n");
        return EXIT_FAILURE;
    }

    if (win_init(&w))
    {
        fprintf(stderr, "SDL窗口初始化失败\n");
        return EXIT_FAILURE;
    }

    if (load_files() != 0)
    {
        fprintf(stderr, "SDL加载 background.png 失败\n");
        return EXIT_FAILURE;
    }

    while (!quit)
    {
        // 处理SDL的事件
        while (SDL_PollEvent(&event))
        {
            win_handle_events(&w);

            if ((event.type == SDL_KEYDOWN) && (event.key.keysym.sym == SDLK_ESCAPE))
            {
                quit = 1;
                printf("处理退出事件\n");
            }

            // 如果用户点击了窗口右上角的关闭按钮
            if (event.type == SDL_QUIT)
            {
                quit = 1;
                printf("处理退出事件\n");
            }
        }

        if (win_error(&w))
        {
            fprintf(stderr, "window error!\n");
            return EXIT_FAILURE;
        }

        // 更新背景
        SDL_FillRect(screen, &screen->clip_rect,
                     SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
        apply_surface((screen->w - testing->w) / 2,
                      (screen->h - testing->h) / 2,
                      testing,
                      screen);
        if (message)
            apply_surface((SCREEN_WIDTH - message->w) / 2,
                          0,
                          message,
                          screen);

        // 更新窗口
        if (SDL_Flip(screen) == -1)
        {
            fprintf(stderr, "SDL更新窗口失败\n");
            break;
        }
    }

    clean_up();

    return 0;
}


/***  Copyright (C) 2022 wy1107412334@163.com All rights reserved ***/
/****************************** END OF FILE *************************/

